package com.app.mingluxing.chat;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.app.mingluxing.R;
import com.app.mingluxing.adapter.ChangeMasterAdapter;
import com.app.mingluxing.base.BaseActivity;
import com.app.mingluxing.bean.GroupSettingBean;
import com.app.mingluxing.bean.MessageBean;
import com.app.mingluxing.mqtt.MqttManager;
import com.app.mingluxing.net.HttpUtil;
import com.app.mingluxing.utils.Constant;

/**
 * 群主管理权转让页面
 * Created by Aaron on 16/4/19.
 */
public class ExchangeMasterActivity extends BaseActivity {

    private ListView lvnums;
    private ImageView ivleft;
    private TextView tvtitle;
    //    private ArrayList<GroupSettingBean.TopicEntity.UserListEntity> userList;
    private GroupSettingBean.TopicEntity topicEntity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_choose_master);

        topicEntity = getIntent().getParcelableExtra("topicEntity");

        initWidget();

    }

    private void initWidget() {

        this.tvtitle = (TextView) findViewById(R.id.tv_title);
        this.ivleft = (ImageView) findViewById(R.id.iv_left);
        this.lvnums = (ListView) findViewById(R.id.lv_nums);

        tvtitle.setText(getResources().getString(R.string.choose_new_master));
        ivleft.setOnClickListener(this);

        if (topicEntity == null)
            return;

        ChangeMasterAdapter adapter = new ChangeMasterAdapter(this, topicEntity.userList);
        lvnums.setAdapter(adapter);

        lvnums.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

                if (topicEntity.userList == null || topicEntity.userList.get(position).user == null)
                    return;

                new AlertDialog.Builder(ExchangeMasterActivity.this).setTitle("注意").setMessage(topicEntity.userList.get(position).user.nickName + "将提升为新群主")
                        .setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                        if (topicEntity.userList.get(position).user != null)

                            deliveryMaster(topicEntity.userList.get(position));
                        else
                            showToast("好友数据异常!");
                    }
                }).show();

            }
        });

    }

    /**
     * 设置群主
     *
     * @param number
     */
    private void deliveryMaster(final GroupSettingBean.TopicEntity.UserListEntity number) {

        if (number == null || number.user == null)
            return;

        if (sp.getString(Constant.USERID, "").equals(number.user.phoneNum)) {
            showToast("您已经是群主");
            return;
        }

        JSONObject j = new JSONObject();
        j.put("phoneNum", number.user.phoneNum);
        j.put("topic", topicEntity.topic);
        j.put("token", sp.getString(Constant.TOKEN, ""));

        HttpUtil.setPostRequest("移交中...", this, Constant.WEB_ROOT_TALK + Constant.UPDATEGROUPADMIN, j.toJSONString(),
                getClass().getName(), new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {
                            org.json.JSONObject result = new org.json.JSONObject(str);

                            if (result.getInt("code") == Constant.STATUS_SUCCESS) {

                                showToast("移交成功");
                                Intent data = new Intent();
                                data.putExtra("newMasterNum", number.user.phoneNum);
                                setResult(0x122, data);

                                MessageBean mb = new MessageBean();
                                mb.messageType = Constant.MQTTMESSAGENOTICE;
                                mb.sendId = sp.getString(Constant.USERID, "");
                                mb.sendPic = sp.getString(Constant.USERIMG, "");
                                mb.sendName = sp.getString(Constant.NICKNAME, "");
                                mb.content = number.user.nickName + " 成为新的群组管理员";
                                mb.channelId = topicEntity.topic;
                                mb.receivePic = topicEntity.groupImgUrl;
                                mb.receiveName = topicEntity.groupName;
//                                mb.chatType = Constant.CHAT_GROUP;
                                mb.sendDate = System.currentTimeMillis();
                                MqttManager.getInstance().setMessage(topicEntity.topic, mb);

                                finish();

                            } else {
                                Constant.showErrorMsg(result.getInt("code"), getApplicationContext());
                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    }
                }, null);
    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:
                finish();
                break;

        }

    }
}
